package com.qf.controller;


import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.GoodsFeign;
import com.qf.pojo.Goods;
import com.qf.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/13 11:09
 * description:
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @RequestMapping("/testCache")
    public String testCache(){
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        orderService.clearCache(1);
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        System.out.println(orderService.testCache(1));
        return "success";
    }

//    @Autowired
//    EurekaClient client;
//
//    @Autowired
//    RestTemplate restTemplate;

    @Autowired
    GoodsFeign goodsFeign;

    @RequestMapping("/sayHello")
    //兜底方法，当处理器方法出现问题时，执行的方法
    @HystrixCommand(fallbackMethod = "fallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")

    })
    public String sayHello(Integer id){
//        InstanceInfo instanceInfo = client.getNextServerFromEureka("QF_GOODS", false);
//        String url = instanceInfo.getHomePageUrl();
//        System.out.println(url); //http://localhost:8080
//        String url="http://QF-GOODS";
//        String action="/goods/sayHello";
//        //返回响应实体
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url+action, String.class);
//        String body = responseEntity.getBody();
//        return "Order Hello!!!---"+body;


//        String url="http://localhost:8080/goods/sayHello";
//        //如何调用goods项目获取goods数据
//        //参数1： url路径 参数2：返回值类型
//        //返回值：响应实体对象
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
//        String body = responseEntity.getBody();
//        return "Order Hello"+body;
//        String goodsBody = goodsFeign.sayHello();
//        return "Order Hello!!!" + goodsBody;

//        Goods goods=new Goods();
//        goods.setId(1);
//        goods.setName(" 华为p40");
//        goods.setPrice(5000.0);
//        String goodsBody = goodsFeign.add(goods);

        String body = goodsFeign.get(id);
        return "Order Hello!!!--"+body;
    }
    public String fallback(Integer id){
        return "服务降级了";
    }
}
